package com.huoyun.order.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.huoyun.base.dao.BaseDaoImpl;
import com.huoyun.order.bean.Order;
import com.huoyun.order.dao.evt.GetOrderForIndexEvt;
import com.huoyun.order.dao.evt.QueryOrderListEvt;
import com.huoyun.order.model.OrderIndexModel;
import com.huoyun.order.model.OrderLightModel;

public class OrderDaoImpl extends BaseDaoImpl<Order> implements OrderDao {

	@Override
	public long saveOrder(Order order) {
		sqlSessionTemplate.insert("com.huoyun.order.dao.OrderDaoMapper.saveOrder", order);
		return order.getId();
	}

	@Override
	public void update(Order order) {
		sqlSessionTemplate.update("com.huoyun.order.dao.OrderDaoMapper.update", order);
	}
	
	@Override
	public void updateOrderStatus(long orderId, int status)
	{
		Map<String, Object> dateMap = new HashMap<String, Object>();
		dateMap.put("orderId", orderId);
		dateMap.put("status", status);
		
		sqlSessionTemplate.update("com.huoyun.order.dao.OrderDaoMapper.updateOrderStatus", dateMap);
	}

	@Override
	public Order getOrderById(long orderId) {
		return sqlSessionTemplate.selectOne("com.huoyun.order.dao.OrderDaoMapper.getOrderById", orderId);
	}

	@Override
	public void delete(long orderId) {
		sqlSessionTemplate.delete("com.huoyun.order.dao.OrderDaoMapper.delete", orderId);
	}
	
	@Override
	public long queryOrderCount(QueryOrderListEvt queryAnnoucemnetPageEvt)
	{
		return sqlSessionTemplate.selectOne("com.huoyun.order.dao.OrderDaoMapper.queryOrderCount", queryAnnoucemnetPageEvt);
	}
	/**
	 * 获取交易总额
	 * @author yang
	 */
	@Override
	public String queryOrderMoney(QueryOrderListEvt queryOrderListEvt) {
		return sqlSessionTemplate.selectOne("com.huoyun.order.dao.OrderDaoMapper.queryOrderMoney",queryOrderListEvt);
	}
	
	@Override
	public List<Order> queryList(QueryOrderListEvt queryOrderListEvt) 
	{	
		return sqlSessionTemplate.selectList("com.huoyun.order.dao.OrderDaoMapper.queryList", queryOrderListEvt);
	}
	
	/**
	 * 查询货物关联的订单信息
	 * @param cargosOwnerId 货物拥有者id
	 * @param cargosId 货物id
	 * 
	 * @author 刘晓宁
	 * @return 订单信息列表
	 */
	public List<Order> queryOrders(long cargosOwnerId, long cargosId)
	{
		Map<String, Object> dataMap = new HashMap<String, Object>();
		dataMap.put("cargosOwnerId", cargosOwnerId);
		dataMap.put("cargosId", cargosId);
		
		return sqlSessionTemplate.selectList("com.huoyun.order.dao.OrderDaoMapper.queryOrdersByCargosOwnerIdAndCargosId", dataMap);
	}

	@Override
	public long getCountByDay(String dateStr) {
		return sqlSessionTemplate.selectOne("com.huoyun.order.dao.OrderDaoMapper.getCountByDay", dateStr);
	}
	
	/**
	 * 删除订单，这里只是将订单的状态标示为：999（标示删除）
	 * @param orderId 订单id
	 * @return 
	 * 
	 * @author 刘晓宁
	 */
	public void deleteOrder(long orderId)
	{
		sqlSessionTemplate.update("com.huoyun.order.dao.OrderDaoMapper.deleteOrder", orderId);
	}

	/**
	 * 获取轻量型order，不包含User信息，OrderLightModel包含货物信息，订单信息，车辆信息
	 * @param queryOrderListEvt
	 * @return
	 * @author yang
	 */
	@Override
	public List<OrderLightModel> queryLightModels(QueryOrderListEvt queryOrderListEvt){
		return sqlSessionTemplate.selectList("com.huoyun.order.dao.OrderDaoMapper.queryLightModels",queryOrderListEvt);
	}
	
	/**
	 * 首页获取正在交易信息 
	 * @author yang
	 */

	@Override
	public List<OrderIndexModel> getOrderForIndex(GetOrderForIndexEvt evt) {
		return sqlSessionTemplate.selectList("com.huoyun.order.dao.OrderDaoMapper.getOrderForIndex",evt);
	}

	

}
